﻿#include "MFormatUtil.h"
#include <stdio.h>



MFormatUtil::MFormatUtil()
{

}

void MFormatUtil::free(MFormat& fmt)
{
    delete fmt.vProp;
    fmt.vProp = NULL;

    delete fmt.config;
    fmt.config = NULL;

    fmt.vPropSize = 0;
    fmt.configSize = 0;
}

void MFormatUtil::copy(const MFormat& srcFmt, MFormat& fmt)
{
	copyVideo(srcFmt, fmt);
	copyAudio(srcFmt, fmt);
}

void MFormatUtil::copyVideo(const MFormat& srcFmt, MFormat& fmt)
{
	fmt.codec = srcFmt.codec;
	fmt.width = srcFmt.width;
	fmt.height = srcFmt.height;
	fmt.framerate = srcFmt.framerate;
	fmt.bitrate = srcFmt.bitrate;
	fmt.profile = srcFmt.profile;
	fmt.clockRate = srcFmt.clockRate;
}

void MFormatUtil::copyAudio(const MFormat& srcFmt, MFormat& fmt)
{
    fmt.audioCodec = srcFmt.audioCodec;
    fmt.channels = srcFmt.channels;
    fmt.sampleRate = srcFmt.sampleRate;
    fmt.audioBitrate = srcFmt.audioBitrate;
    fmt.audioProfile = srcFmt.audioProfile;
    fmt.audioRate = srcFmt.audioRate;
}

static void copymem(const uint8_t* src, size_t size, uint8_t*& dest, int& length)
{
    if (size > 0)
    {
        if (dest && src)
        {
            length = (length > (int)size) ? size : length;
            memcpy(dest, src, length);
        }
        else
        {
            length = 0;
        }
    }
    else
    {
        length = 0;
    }
}

void MFormatUtil::copyFull(const MFormat& srcFmt, MFormat& fmt)
{
    copy(srcFmt, fmt);

    copymem(srcFmt.vProp, srcFmt.vPropSize, fmt.vProp, fmt.vPropSize);
    copymem(srcFmt.config, srcFmt.configSize, fmt.config, fmt.configSize);
}

bool MFormatUtil::check(const MFormat& fmt)
{
	return (fmt.codec != 0) || (fmt.audioCodec != 0);
}


void MFormatUtil::copyTo(const MFormat* fmt, av::MediaFormat& mediaFormat)
{
    mediaFormat.m_codec = (av::MediaCodec)fmt->codec;
    mediaFormat.m_width= fmt->width;
    mediaFormat.m_height = fmt->height;
    mediaFormat.m_framerate = fmt->framerate;
    mediaFormat.m_profile = fmt->profile;
    mediaFormat.m_clockRate = fmt->clockRate;
    mediaFormat.m_bitrate = fmt->bitrate;

    mediaFormat.m_audioCodec = (av::MediaCodec)fmt->audioCodec;
    mediaFormat.m_channels = fmt->channels;
    mediaFormat.m_sampleRate = fmt->sampleRate;
    mediaFormat.m_audioProfile = fmt->audioProfile;
    mediaFormat.m_audioRate = fmt->audioRate;
    mediaFormat.m_audioBitrate = fmt->audioBitrate;

    if (fmt->vProp && fmt->vPropSize > 0)
    {
        mediaFormat.m_videoProp.assign((char*)fmt->vProp, fmt->vPropSize);
    }

    if (fmt->config && fmt->configSize > 0)
    {
        mediaFormat.m_audioConfig.assign((char*)fmt->config, fmt->configSize);
    }
}

void MFormatUtil::copyFrom(MFormat* fmt, const av::MediaFormat& mediaFormat)
{
    fmt->codec = mediaFormat.m_codec;
    fmt->width = mediaFormat.m_width;
    fmt->height = mediaFormat.m_height;
    fmt->framerate = mediaFormat.m_framerate;
    fmt->profile = mediaFormat.m_profile;
    fmt->clockRate = mediaFormat.m_clockRate;

    fmt->audioCodec = mediaFormat.m_audioCodec;
    fmt->channels = mediaFormat.m_channels;
    fmt->sampleRate = mediaFormat.m_sampleRate;
    fmt->audioProfile = mediaFormat.m_audioProfile;
    fmt->audioRate = mediaFormat.m_audioRate;

    fmt->vProp = (uint8_t*)mediaFormat.m_videoProp.c_str();
    fmt->vPropSize = mediaFormat.m_videoProp.size();

    fmt->config = (uint8_t*)mediaFormat.m_audioConfig.c_str();
    fmt->configSize = mediaFormat.m_audioConfig.size();
}
